package com.oocl.euc.core.requestform.repository;

import com.oocl.euc.core.builder.*;
import com.oocl.euc.core.common.ITTestBase;
import com.oocl.euc.core.requestform.model.*;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;


public class RequestFormRepositoryIT extends ITTestBase {

    @Autowired
    RequestFormBuilder requestFormBuilder;
    @Autowired
    RequestPostmortemBuilder requestPostmortemBuilder;
    @Autowired
    RequestProceduresBuilder requestProceduresBuilder;

    @Autowired
    RequestFormRepository requestFormRepository;
    @Autowired
    RequestProceduresRepository requestProceduresRepository;
    @Autowired
    RequestPostmortemRepository requestPostmortemRepository;


    @After
    public void tearDown() {

        requestPostmortemRepository.deleteAll();
        requestProceduresRepository.deleteAll();
        requestFormRepository.deleteAll();
    }

    @Test
    public void should_return_created_requestform_when_given_requestform(){

        RequestPostmortem requestPostmortem = requestPostmortemBuilder.defaultBuilder()
                .create();

        RequestProcedures requestProcedures = requestProceduresBuilder.defaultBuilder()
                .withProcedureDesc("Procedures Desc")
                .withProcedureSeq(0)
                .create();

        RequestForm requestForm = requestFormBuilder.defaultBuilder()
                .withRequestPostmortems(Arrays.asList(requestPostmortem))
                .withRequestprocedures(Arrays.asList(requestProcedures))
                .withRefNO("000111")
                .create();

        requestForm = requestFormRepository.save(requestForm);
        Assert.assertTrue(requestForm!=null);
        Assert.assertTrue(requestForm.getRequestpostmortem().size()==1);
        Assert.assertTrue(requestForm.getRequestprocedures().size()==1);
        Assert.assertTrue("000111".equals(requestForm.getRefNO()));
        Assert.assertTrue("LINCO".equals(requestForm.getRequesterDomainId()));

    }

    @Test
    public void should_return_a_requestform_when_given_a_refNO(){

        RequestForm requestForm = requestFormBuilder.defaultBuilder()
                .withRefNO("000123")
                .withRequestDomainID("LINCO")
                .saveToDB();
        RequestForm result = requestFormRepository.findByRefNO("000123");
        Assert.assertTrue(result != null);
        Assert.assertTrue("000123".equals(result.getRefNO()));
        Assert.assertTrue("LINCO".equals(result.getRequesterDomainId()));
    }

}
